import pygame.mouse
import image
import sun
import card
import sunFlower
import peaBullet
import pea
import cell
import way
import zombie1
from data import *
from constant import *

class Game(object):
    def __init__(self,surface):
        self.surface = surface
        # 背景
        self.bgImage = image.Image(PROJECT_DIR + BG_IMAGE_PATH, SCREEN_SIZE,BG_POSITION)
        #卡片
        self.cards = []
        self.init_card()
        #格子
        self.cells = []
        self.init_cell()
        #跑道
        self.ways = []
        self.init_way()
        #植物
        self.plants = []
        #僵尸
        self.zombies = []
        #召唤物
        self.summons = []
        #当前种植的植物
        self.current_plant = None
        #当前种植的僵尸
        self.current_zombie = None

    #初始化卡片
    def init_card(self):
        cardPos = data.get('card').get('pos')
        cardSize = data.get('card').get('size')
        for cardInfo in data.get('card').get('info'):
            self.cards.append(card.Card(cardInfo.get('path'), cardSize ,cardPos,cardInfo.get('id')))
            cardPos = (cardPos[0] + cardSize[0] + 5,cardPos[1])

    #初始化格子（植物用）
    def init_cell(self):
        for i in range(GRID_COUNT[0]):
            for j in range(GRID_COUNT[1]):
                x = LEFT_TOP[0] + i * GRID_SIZE[0]
                y = LEFT_TOP[1] + j * GRID_SIZE[1]
                self.cells.append(cell.Cell((x,y)))

    #初始化道路（僵尸用）
    def init_way(self):
        for i in range(GRID_COUNT[1]):
            x = SCREEN_SIZE[0]
            y = LEFT_TOP[1]-30 + i * GRID_SIZE[1]
            self.ways.append(way.Way((x,y)))

    #绘制背景
    def draw_background(self):
        self.bgImage.draw(self.surface)

    #绘制卡片
    def draw_card(self):
        for card in self.cards:
            card.draw(self.surface)

    #绘制植物
    def plant_grow(self) :
        for plant in self.plants:
            summon = plant.summon()
            if summon is not None:
                self.summons.append(summon)
            plant.grow()

    #绘制召唤物
    def summons_move(self):
        for summon in self.summons:
            if summon.checkDead():
                self.summons.remove(summon)
                summon.kill()
            else:
                summon.move()

    #绘制僵尸
    def zombie_move(self):
        for zombie in self.zombies:
            if zombie.is_dead():
                self.zombies.remove(zombie)
                zombie.kill()
            else:
                for summon in self.summons:
                    if isinstance(summon,peaBullet.PeaBullet):
                        if self.check_image_pos(summon.pos,zombie.pos):
                            summon.liveTime = 0
                            zombie.fight(summon.atk)
                zombie.move()

    #用来判断召唤物的位置
    @staticmethod
    def check_image_pos(current, target):
        in_pos =  False
        if target[0] <= current[0] <= target[0] + 50 and target[1] <= current[1] <= target[1] + 50:
            in_pos = True
        return in_pos

    #用来判断格子的位置
    @staticmethod
    def check_cell_pos(current, target):
        in_pos =  False
        if target[0] <= current[0] <= target[0] + GRID_SIZE[0] and target[1] <= current[1] <= target[1] + GRID_SIZE[1]:
            in_pos = True
        return in_pos

    #用来判断卡片的位置
    @staticmethod
    def check_card_pos(current, target):
        size = data.get('card').get('size')
        in_pos =  False
        if target[0] <= current[0] <= target[0] + size[0] and target[1] <= current[1] <= target[1] + size[1]:
            in_pos = True
        return in_pos

    # 检查并拾取召唤物（后期把阳光和子弹分开放，提升效率）
    def check_mouse_summons(self,pos):
        for summon in self.summons:
            if isinstance(summon,sun.Sun):
                if self.check_image_pos(pos,summon.pos):
                    summon.pick()

    # 检查并种植植物
    def check_mouse_plants(self,pos):
        if self.current_plant is not None:
            for cell in self.cells:
                if cell.has_plant:
                    continue
                if self.check_cell_pos(pos,cell.pos):
                    if self.current_plant == SUN_FLOWER:
                        self.plants.append(sunFlower.SunFlower(self.surface, cell.pos))
                        cell.has_plant = True
                        self.current_plant = None
                    elif self.current_plant == PEA:
                        self.plants.append(pea.Pea(self.surface, cell.pos))
                        cell.has_plant = True
                        self.current_plant = None

    # 检查鼠标是否点击僵尸
    def check_mouse_zombie(self,pos):
        if self.current_zombie is not None:
            for way in self.ways:
                self.zombies.append(zombie1.Zombie1(self.surface, way.pos))
            self.current_zombie = None

    # 点击卡片
    def check_mouse_card(self,pos):
        for card in self.cards:
            if self.check_card_pos(pos,card.pos):
                #id带zombie视为僵尸卡牌
                if ZOMBIE in card.id :
                    self.current_zombie = card.id
                else:
                    self.current_plant = card.id

    #鼠标点击动作
    def mouse_down(self):
        mouse_pos = pygame.mouse.get_pos()
        self.check_mouse_card(mouse_pos)
        self.check_mouse_summons(mouse_pos)
        self.check_mouse_plants(mouse_pos)
        self.check_mouse_zombie(mouse_pos)





